package cn.jji8.createX.function.fill.canPlaceBlock;

import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class FillCanPlaceBlockState {
    static final Map<Player,FillCanPlaceBlockState> map = new HashMap<>();
    /**
     * 玩家退出时取消掉
     * */
    public static void playerQuitEvent(PlayerQuitEvent e){
        FillCanPlaceBlockState fillCanPlaceBlockState = map.remove(e.getPlayer());
        if(fillCanPlaceBlockState!=null){
            fillCanPlaceBlockState.setCancel(true);
        }
    }
    /*                取消     完成        */
    private boolean cancel,complete;
    final Player player;

    @SuppressWarnings("unused")
    public FillCanPlaceBlockState(Player player, boolean cancel, boolean complete) {
        this.cancel = cancel;
        this.complete = complete;
        this.player =player;
        map.put(player,this);
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
        if(cancel){
            map.remove(player);
        }else {
            map.put(player,this);
        }
    }

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
        if(complete){
            map.remove(player);
        }else {
            map.put(player,this);
        }
    }

    long lastTiem = 0;
    long sendMessage = 0;
    /**
     * 给玩家发送粒破坏子效果，但不会发送的过于的多,避免卡客户端
     * */
    public void playEffectSTEP_SOUND(Location location){
        if(System.currentTimeMillis()<lastTiem+50){
            return;
        }
        lastTiem = System.currentTimeMillis();
        Objects.requireNonNull(location.getWorld()).playEffect(location, Effect.STEP_SOUND,location.getBlock().getType());
    }
}
